package com.example.tourpal.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tourpal.R;
import com.example.tourpal.user.RoundImageView;

public class HomePlanAdapter extends BaseAdapter {

	private static final String UNDER_LINE = "-";

	private Context context;

	private List<HashMap<String, Object>> dataSourceList = new ArrayList<HashMap<String, Object>>();
	private LayoutInflater mLayoutInflater;

	private boolean haveAD;

	public final class ItemView {
		public ImageView advertisement;

		public LinearLayout planList;
		public RoundImageView headPic;
		public ImageView followSign;
		public TextView planName;
		public TextView planDate;
		public LinearLayout planHint;
		public TextView planDescription;
		public TextView goodCnt;
		public TextView commentCnt;
	}

	public HomePlanAdapter(Context context,
			List<HashMap<String, Object>> dataSourceList, boolean advert) {
		this.context = context;
		this.dataSourceList = dataSourceList;
		mLayoutInflater = LayoutInflater.from(context);
		haveAD = advert;
	}

	public void setListItem(List<HashMap<String, Object>> newList) {
		dataSourceList = newList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataSourceList.size();
	}

	@Override
	public Object getItem(int i) {
		// TODO Auto-generated method stub
		return dataSourceList.get(i);
	}

	@Override
	public long getItemId(int i) {
		// TODO Auto-generated method stub
		return dataSourceList.get(i).hashCode();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewGroup root = null;
		ItemView itemView = null;
		if (convertView == null) {
			itemView = new ItemView();

			convertView = mLayoutInflater
					.inflate(R.layout.home_plan_item, root);

			itemView.advertisement = (ImageView) convertView
					.findViewById(R.id.advertisement);

			itemView.planList = (LinearLayout) convertView
					.findViewById(R.id.planList);
			itemView.headPic = (RoundImageView) convertView
					.findViewById(R.id.headPic);
			itemView.followSign = (ImageView) convertView
					.findViewById(R.id.followSign);
			itemView.planName = (TextView) convertView
					.findViewById(R.id.planName);
			itemView.planDate = (TextView) convertView
					.findViewById(R.id.planDate);
			itemView.planHint = (LinearLayout) convertView
					.findViewById(R.id.planHint);
			itemView.planDescription = (TextView) convertView
					.findViewById(R.id.planDescription);
			itemView.goodCnt = (TextView) convertView.findViewById(R.id.good);
			itemView.commentCnt = (TextView) convertView
					.findViewById(R.id.comment);

			convertView.setTag(itemView);
		} else {
			itemView = (ItemView) convertView.getTag();
		}

		HashMap<String, Object> item = dataSourceList.get(position);

		if (position == 0) {
			if (haveAD) {
				itemView.advertisement.setVisibility(View.VISIBLE);
			} else {
				itemView.advertisement.setVisibility(View.GONE);
			}
			itemView.planList.setVisibility(View.GONE);

		} else {
			itemView.advertisement.setVisibility(View.GONE);
			itemView.planList.setVisibility(View.VISIBLE);

			convertView.setId((Integer) item.get("planId"));

			int headPicResId = (Integer) item.get("headPic");
			int followSign = (Integer) item.get("followSign");
			String planName = (String) item.get("planName");
			String planDate = (String) item.get("planDate");
			String planHint = (String) item.get("planHint");
			String[] hintList = planHint.split(UNDER_LINE);
			int goodCnt = (Integer) item.get("goodCnt");
			int commentCnt = (Integer) item.get("commentCnt");

			itemView.headPic.setImageResource(headPicResId);
			if (followSign == 1) {
				itemView.followSign
						.setBackgroundResource(R.drawable.recruit_sign_middle);
			}

			itemView.planName.setText(planName);
			itemView.planDate.setText(String.format(context.getResources()
					.getString(R.string.home_date), planDate));
			itemView.planHint.removeAllViews();
			itemView.planHint.setGravity(Gravity.CENTER_VERTICAL);
			TextView hintTitle = new TextView(context);
			hintTitle.setTextColor(0xff4c4c4c);
			hintTitle.setTextSize(14);
			hintTitle.setText(context.getResources().getString(R.string.home_spot));
			itemView.planHint.addView(hintTitle);
			for (int i = 0; i < hintList.length; i++) {
				if (hintList.length > 1 && i == hintList.length - 1) {
					itemView.planDescription.setText(hintList[i]);
					break;
				} else {
					TextView hint = new TextView(context);
					hint.setTextColor(0xff4c4c4c);
					hint.setTextSize(14);
					hint.setText(hintList[i]);
					itemView.planHint.addView(hint);
				}

				if (hintList.length > 2 && i != hintList.length - 2) {
					ImageView link = new ImageView(context);
					link.setBackgroundResource(R.drawable.link_1);
					itemView.planHint.addView(link);
				}

			}
			itemView.goodCnt.setText(String.valueOf(goodCnt));
			itemView.commentCnt.setText(String.valueOf(commentCnt));

		}
		return convertView;
	}

}
